package servlet;

import constant.ExamConstant;
import model.Doctor;
import model.Patient;
import service.DoctorService;
import service.PatientService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserLogin extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String type = request.getParameter("type");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //判断登录是否成功
        boolean loginFlag = false;
        System.out.println(type);
        System.out.println(username);
        System.out.println(password);
        //判断参数
        if (type != null && username !=null && password !=null && type.trim().length() >0
                && username.trim().length()>0 && password.trim().length()>0){
            //[0-医生，1-病人] 登陆成功则修改登陆标识与跳转页面地址，同时传递对应页面默认展示的数据
            if(ExamConstant.LOGIN_TYPE_DOCTOR.equals(type)){
                Doctor model = DoctorService.login(username,password);
                if (model != null){
                    loginFlag = true;
                    request.getSession().setAttribute(ExamConstant.SESSION_LOGIN_USER,model);
                }
            }else if (ExamConstant.LOGIN_TYPE_PATIENT.equals(type)){
                Patient model = PatientService.login(username,password);
                if(model != null){
                    loginFlag = true;
                    request.getSession().setAttribute(ExamConstant.SESSION_LOGIN_USER,model);
                }
            }
        }
        System.out.println(loginFlag);
        //根据标识来分流登陆成功与失败
        if (loginFlag){
            request.getSession().setAttribute(ExamConstant.SESSION_LOGIN_TYPE,type);
            response.sendRedirect(ExamConstant.HOME_PAGE_QUERY);
        }else {
            request.setAttribute("error","用户名或密码错误，请重新填写");
            request.getRequestDispatcher(ExamConstant.HOME_PAGE_LOGIN).forward(request,response);
        }
    }
}

